#include "test8.h"
#include "ui_test8.h"

test8::test8(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::test8)
{
    ui->setupUi(this);
    initital();
}

test8::~test8()
{
    delete ui;
}

void test8::initital()
{
    QStringList table_label;
    table_label<<"序号"<<"数字2"<<"数字3"<<"数字4"<<"数字5";
    my_Model = new QStandardItemModel(ui->treeView);
    my_Model->setHorizontalHeaderLabels(table_label);

    for(int i=0;i<2;i++)
    {
        QList<QStandardItem*> items1;
        QStandardItem* item1 = new QStandardItem(QString::number(i+1));
        items1.append(item1);
        for(int i =0;i<4;i++)
        {
            items1.append(new QStandardItem(QString::number(qrand()%50)));
        }
        my_Model->appendRow(items1);

        for(int j=0;j<4;j++)
        {
            QList<QStandardItem*> items2;
            QStandardItem* item2 = new QStandardItem(QString::number(j+1));
            items2.append(item2);
            for(int i =0;i<4;i++)
            {
                items2.append(new QStandardItem(QString::number(qrand()%50)));
            }
            item1->appendRow(items2);
        }
    }
    ui->treeView->setModel(my_Model);
}

void test8::on_pushButton_clicked()
{

    for(int i = 0;i < my_Model->rowCount() ;i++)
    {
        QStandardItem *item = my_Model->item(i);
        querytree(item);
    }
}

void test8::querytree(QStandardItem *item)
{
    for(int j=1;j<5;j++)
    {
      int data =  my_Model->data(my_Model->index(item->index().row(),j,item->index().parent())).toString().toInt();
      if(data<25)
         my_Model->setData(my_Model->index(item->index().row(),j,item->index().parent()),QString::number(0));
    }

    if(item->hasChildren())
    {
        for(int i = 0;i < item->rowCount() ;i++)
        {
            QStandardItem * childitem = item->child(i);
            querytree(childitem);
        }
    }
}

void test8::deletenode(QStandardItem *item)
{
    if(item->hasChildren())
    {
        for(int i = item->rowCount()-1;i >= 0 ;i--)
        {
            QStandardItem * childitem = item->child(i);
            deletenode(childitem);
        }
    }
    else
    {
        my_Model->removeRow(item->index().row(),item->index().parent());
    }
    my_Model->removeRow(item->index().row(),item->index().parent());
}

void test8::deletetree(QStandardItem *item)
{
    if(item->hasChildren())
    {
        for(int i = item->rowCount()-1;i >= 0 ;i--)
        {
            QStandardItem * childitem = item->child(i);
            deletetree(childitem);
        }
    }
    else
    {
        if(item->index().row()%2)
        {
            my_Model->removeRow(item->index().row(),item->index().parent());
        }
    }
}

void test8::deletetree2(QStandardItem *item)
{
    if(item->index().row()%2)
    {
        deletenode(item);
    }

    if(item->hasChildren())
    {
        for(int i = item->rowCount()-1;i >= 0 ;i--)
        {
            QStandardItem * childitem = item->child(i);
            deletetree2(childitem);
        }
    }
}

void test8::on_pushButton_2_clicked()
{
    for(int i = 0;i < my_Model->rowCount() ;i++)
    {
        QStandardItem *item = my_Model->item(i);
        deletetree(item);
    }
}

void test8::on_pushButton_3_clicked()
{
    initital();
}

void test8::on_pushButton_4_clicked()
{
    for(int i =  my_Model->rowCount()-1;i >=0 ;i--)
    {
        QStandardItem *item = my_Model->item(i);
        deletetree2(item);
    }
}
